<?php
/**
 * 大商创吉客云所有订单下载
 * auth 孙凯
 * date 20201223
 */

namespace App\Http\Controllers\Dsc\Model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Orderinfo extends Model
{
    protected $table = 'order_info';

    public function ordergoods()
    {
        return $this->hasOne('App\Http\Controllers\Dsc\Model\Ordergoods', 'order_id', 'order_id');
    }

    /**
     * 大商创所有订单下载到吉客云接口
     * auther sk
     * date 20201223 17:44
     * @param $res
     * @return array
     */
    public function get_orders_data($res)
    {
        $arr = [];
        foreach ($res as $k => $v) {
//            Log::channel('cache')->info('imsk' . json_encode($v['PlatOrderNo']));
            $ordergoodsselect = [
                'goods_sn as ProductId',
                'goods_id as tradegoodsno',
                'goods_name as tradegoodsname',
                'goods_number as goodscount',
                'goods_price as price',
//                'shipping_fee as postfee',
                'goods_attr as tradegoodsspec'
            ];
            $goodinfos = mysql_cont('order_goods')->where(['order_id' => $v['order_id']])
                ->select($ordergoodsselect)
                ->get();
            $goodinfos = objectToArray($goodinfos);
            $goods_data = [];
            $goodsAll_data = [];
            foreach ($goodinfos as $val) {
                $goods_data['ProductId'] = $val['ProductId'];
                $goods_data['tradegoodsno'] = $val['ProductId'];
                $goods_data['suborderno'] = $v['PlatOrderNo'];
                $goods_data['tradegoodsname'] = $val['tradegoodsname'];
                $goods_data['tradegoodsspec'] = trim($val['tradegoodsspec']);
                $goods_data['goodscount'] = $val['goodscount'];
                $goods_data['price'] = $val['price'];
                $goodsAll_data[] = $goods_data;
            }

            $data['PlatOrderNo'] = $v['PlatOrderNo'];
            $data['tradeStatus'] = checkStatus($v['tradeStatus'], $v['shipping_status'], $v['pay_status']);
            $data['tradetime'] = date('Y-m-d H:i:s', ($v['add_time'] + 60 * 60 * 8));
            $data['province'] = Region::returnCity($v['province']);
            $data['city'] = Region::returnCity($v['city']);
            $data['aray'] = Region::returnCity($v['district']);
            $data['address'] = $v['address'];
            $data['mobile'] = $v['mobile'];
            $data['customerremark'] = $v['customerremark'];
            $data['postfee'] = $v['postfee'];
            $data['goodsfee'] = $v['goodsfee'];
            $data['totalmoney'] = $v['totalmoney'];
            $data['realpaymoney'] = $v['realpaymoney'];
            $data['favourablemoney'] = $v['favourablemoney'];//优惠的金额
            $data['paytime'] = $v['paytime'] == 0 ? '' : date('Y-m-d H:i:s', ($v['paytime'] + 60 * 60 * 8));
            $data['receivername'] = $v['receivername'];
            $nick = Users::getusers($v['user_id']);
            $nick = objectToArray($nick);
            $data['nick'] = $nick['nick_name'];
            $data['ShouldPayType'] = '担保交易';
            $data['logisticno'] = $v['logisticno'];
            $data['logisticname'] = $v['logisticname'];
            $data['goodinfos'] = $goodsAll_data;
            $arr[] = $data;
        }
        return $arr;
    }

    /**
     * 获取订单信息
     * @param $order_id
     * @param $select
     * @return Model|\Illuminate\Database\Query\Builder|object|null
     */
    public static function get_order_info($where, $select = '*')
    {
        return mysql_cont('order_info')->where($where)
            ->select($select)
            ->first();
    }

    public static function get_info($table, $where, $select = '*')
    {
        return mysql_cont($table)->where($where)
            ->select($select)
            ->first();
    }

    /**
     * 更新订单数据
     * auther 孙凯
     * date 20201228
     * @param $where
     * @param $update_data
     * @return int
     */
    public static function update_order($where, $update_data)
    {
        return mysql_cont('order_info')->where($where)->update($update_data);
    }

    /**
     * 查看订单是否存在
     * auther 孙凯
     * date20201230 13:44
     * @param $table tablename
     * @param $where where
     * @return bool true false
     */
    public static function check_exites($table, $where)
    {
        return mysql_cont($table)->where($where)->exists();
    }


    /**
     * 插入数据信息
     * auther 孙凯
     * date20201231 1454
     * @param $table
     * @param $data
     * @return int
     */
    public static function check_insert($table, $data)
    {
        return mysql_cont($table)->insertGetId($data);
    }


}